package com.mashibing.servicedriveruser.controller;

import org.mashibing.constant.DriverCarConstant;
import org.mashibing.dto.DriverUser;
import com.mashibing.servicedriveruser.service.DriverUserService;
import org.mashibing.dto.ResponseResult;
import org.mashibing.response.DriverUserExistsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:
 * @create: 2024/4/26 10:25
 * @description:
 */
@RestController
public class UserController {

    @Autowired
    private DriverUserService driverUserService;

    /**
     * 新增司机信息
     */
    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser) {
        return driverUserService.addUser(driverUser);
    }

    /**
     * 修改司机信息
     */
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser) {
        return driverUserService.updateUser(driverUser);
    }

    /**
     * 查询司机信息
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> getUser(@PathVariable String driverPhone) {
        DriverUser driverUserByPhone = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUserExistsResponse response = new DriverUserExistsResponse();
        response.setDriverPhone(driverPhone);

        int ifExists = DriverCarConstant.DRIVER_EXISTS;
        if (driverUserByPhone == null) {
            ifExists = DriverCarConstant.DRIVER_NOT_EXISTS;
        }

        response.setIfExists(ifExists);
        return ResponseResult.success(response);
    }
}
